package org.jeecg.modules.demo.mes.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.demo.mes.entity.MesMaterialDetail;
import org.jeecg.modules.demo.mes.mapper.MesMaterialDetailsMapper;
import org.jeecg.modules.demo.mes.service.IMesMaterialDetailsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 物料明细表
 * @Author: jeecg-boot
 * @Date: 2022-10-27
 * @Version: V1.0
 */
@Service
public class MesMaterialDetailsServiceImpl extends ServiceImpl<MesMaterialDetailsMapper, MesMaterialDetail> implements IMesMaterialDetailsService {

    /**
     * 通用查询
     */
    @Override
    public List<MesMaterialDetail> queryMaterialDetailsSelective(Map<String, Object> cxMap) {
        if (cxMap == null) {
            return new ArrayList<>();
        }
        return this.baseMapper.queryMaterialDetailsSelective(cxMap);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void removeByMainId(String mainId) throws JeecgBootException {
        this.baseMapper.removeByMainId(mainId);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void resetMaterialDetailByProcedureMaterialBomId(String procedureMaterialBomId) throws JeecgBootException {
        this.baseMapper.resetMaterialDetailByProcedureMaterialBomId(procedureMaterialBomId);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void changeMaterialStatus(String status, String procedureId, List<String> materialBarCodeList, String workOrderDetailId) {
        if (StringUtils.isBlank(status)) {
            return;
        }
        if (materialBarCodeList == null || materialBarCodeList.size() == 0) {
            return;
        }
        this.baseMapper.changeMaterialStatus(status, procedureId, materialBarCodeList, workOrderDetailId);
    }

    @Override
    public MesMaterialDetail queryMaterialDetailByMaterialBarCode(String materialBarCode) {
        if (StringUtils.isBlank(materialBarCode)){
            return null;
        }
        final Map<String, Object> cxMap = new HashMap<>();
        cxMap.put("materialBarCode", materialBarCode);
        final List<MesMaterialDetail> details = queryMaterialDetailsSelective(cxMap);
        if (details != null && details.size() > 0) {
            return details.get(0);
        }
        return null;
    }
}
